package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.sso.enums.SexEnum;
import com.tanhua.sso.mapper.UserInfoMapper;
import com.tanhua.sso.pojo.User;
import com.tanhua.sso.pojo.UserInfo;
import com.tanhua.sso.vo.PicUploadResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Author YuShiLong
 * @Date 2021/4/13 10:10
 * @Version 1.0
 */

@Service
@Slf4j
public class UserInfoService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoMapper userInfoMapper;

//    @Autowired
//    private FaceEngineService faceEngineService;

    @Autowired
    private PicUploadService picUploadService;

    public boolean saveUserInfo(Map<String, String> param, String token) {
        //校验token,判断是否是有效的token,需要在userService里面校验,判断有没有这个token,然后根据这个token找到该用户
        User user = userService.queryUserToken(token);

        //如果返回的user为空,为校验失败
        if (null == user) {
            return false;
        }

        //不为空,定义一个userInfo,并设置值
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setSex(StringUtils.equalsIgnoreCase(param.get("gender"), "man") ? SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setNickName(param.get("nickname"));
        userInfo.setBirthday(param.get("birthday"));
        userInfo.setCity(param.get("city"));

        //调用mapper接口,将用户数据存入数据库,并判断影响行数,是否为一,为一,代表保存成功
        return userInfoMapper.insert(userInfo) == 1;
    }

//    public Boolean saveUserLogo(MultipartFile headPhoto, String token) {
//
//        //校验token,判断是否是有效的token,需要在userService里面校验,判断有没有这个token
//        User user = userService.queryUserToken(token);
//
//        //如果返回的user为空,为校验失败
//        if (null == user) {
//            return false;
//        }
//
//        //判断是否为人像
//        try {
//            //调用虹软接口的方法,判断是否为人像
//            boolean b = faceEngineService.checkIsPortrait(headPhoto.getBytes());
//            //如果不是人像,返回false
//            if (!b) {
//                return false;
//            }
//        } catch (IOException e) {
//            log.error("人像识别识别!", e);
//            return false;
//        }
//        //是人像,上传到阿里云oss
//        PicUploadResult result = picUploadService.upload(headPhoto);
//
//        //判断上传的结果
//        if (StringUtils.isEmpty(result.getName())) {
//            return false;
//        }
//
//        //上传成功,将用户头像上传到数据库
//        UserInfo userInfo = new UserInfo();
//        userInfo.setLogo(result.getName());
//
//        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id", user.getId());
//
//        return this.userInfoMapper.update(userInfo, queryWrapper) == 1;
//    }
}
